package com.wanhe.vip.module.info;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.trello.rxlifecycle2.LifecycleTransformer;
import com.wanhe.vip.R;
import com.wanhe.vip.api.RetrofitService;
import com.wanhe.vip.api.bean.CardInfo;
import com.wanhe.vip.api.bean.VipCard;
import com.wanhe.vip.api.bean.VipCoupon;
import com.wanhe.vip.injector.components.DaggerVipInfoComponent;
import com.wanhe.vip.module.adapter.VipCouponAdapter;
import com.wanhe.vip.widget.EmptyLayout;

import java.util.ArrayList;

import javax.inject.Inject;


/**
 * Created by XieZaiWei on 2017/4/18.
 * 会员Fragment
 */

public class VipFragment extends Fragment implements View.OnClickListener, IVipView {

	private static final String KEY_BILL_AMOUNT = "billAmount";

	private View mView;
	private RelativeLayout rl_login;
	private RelativeLayout rl_info;
	private EditText et_account;
	private EditText et_pwd;
	private TextView tv_name;
	private TextView tv_phone;
	private TextView tv_score;
	private TextView tv_balance;
	private TextView tv_birthday;
	private EditText et_prePay;
	private EditText et_cash;
	private GridView gv_vipCoupon;

	private String mCardNo;

	private VipCouponAdapter mVipCouponAdapter;
	private VipCoupon mVipCoupon;

	/**
	 * 本账单合计总金额
	 */
	private String mBillAmount;
	/**
	 * 预存金额
	 */
	private String mPrePay;
	/**
	 * 现金
	 */
	private String mCash;

	@Inject
	public VipInfoPresenter mVipInfoPresenter;

	public static VipFragment newInstance(String billAmount) {
		VipFragment fragment = new VipFragment();
		Bundle bundle = new Bundle();
		bundle.putString(KEY_BILL_AMOUNT, billAmount);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mBillAmount = getArguments().getString(KEY_BILL_AMOUNT);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		RetrofitService.init(getActivity());
		DaggerVipInfoComponent.builder().build().inject(this);
		mView = inflater.inflate(R.layout.vip_fragment, container, false);
		mVipInfoPresenter.setView(this);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		rl_login = (RelativeLayout) mView.findViewById(R.id.rl_login);
		rl_info = (RelativeLayout) mView.findViewById(R.id.rl_info);
		et_account = (EditText) mView.findViewById(R.id.et_account);
		et_pwd = (EditText) mView.findViewById(R.id.et_pwd);
		tv_name = (TextView) mView.findViewById(R.id.tv_name);
		tv_phone = (TextView) mView.findViewById(R.id.tv_phone);
		tv_score = (TextView) mView.findViewById(R.id.tv_score);
		tv_balance = (TextView) mView.findViewById(R.id.tv_balance);
		tv_birthday = (TextView) mView.findViewById(R.id.tv_birthday);
		et_prePay = (EditText) mView.findViewById(R.id.et_prePay);
		et_cash = (EditText) mView.findViewById(R.id.et_cash);
		gv_vipCoupon = (GridView) mView.findViewById(R.id.gv_vipCoupon);


		mView.findViewById(R.id.btn_login).setOnClickListener(this);
		mView.findViewById(R.id.btn_reset).setOnClickListener(this);
		mView.findViewById(R.id.btn_ok).setOnClickListener(this);

		//会员优惠券
		mVipCouponAdapter = new VipCouponAdapter(getContext());
		gv_vipCoupon.setAdapter(mVipCouponAdapter);
		gv_vipCoupon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mVipCoupon = mVipCouponAdapter.getItem(position);
				//选中的高亮
				for (int i = 0; i < mVipCouponAdapter.getCount(); i++) {
					if (i == position) {
						mVipCouponAdapter.getItem(i).setSelect(true);
					} else {
						mVipCouponAdapter.getItem(i).setSelect(false);
					}
				}
				mVipCouponAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_login) {//登录，即获得会员卡列表
			getVipCardList();

		}else if(id == R.id.btn_ok){//点击确定进行会员预支付
			mPrePay = et_prePay.getText().toString().trim();
			mCash = et_cash.getText().toString().trim();
			if ("".equals(mPrePay) && "".equals(mCash) && mVipCoupon == null) {
				Toast.makeText(getActivity(), "请选择输入抵扣,金额或选择一种优惠券!", Toast.LENGTH_LONG).show();
				return;
			}
			mVipInfoPresenter.preConsume(mCardNo, mBillAmount, mPrePay, mCash, mVipCoupon);

		}

	}

	/**
	 * 登录
	 */
	private void getVipCardList() {
		String phone = et_account.getText().toString();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(getActivity(), "请输入手机号码", Toast.LENGTH_SHORT).show();
			return;
		}
		mVipInfoPresenter.getVipCardList(phone);
	}

	@Override
	public void showCardList(ArrayList<VipCard> arrVipCard) {
		VipCardListDialog vipCardListDialog = new VipCardListDialog(getContext(), arrVipCard, mVipInfoPresenter);
		vipCardListDialog.show();
	}

	@Override
	public void showCardInfo(CardInfo cardInfo) {
		rl_login.setVisibility(View.GONE);
		rl_info.setVisibility(View.VISIBLE);
		mCardNo = cardInfo.getCardNo();
		tv_name.setText("会员: " + cardInfo.getMemberName());
		tv_phone.setText("手机: " + cardInfo.getMobile());
		tv_score.setText("积分: " + cardInfo.getPoint());
		tv_balance.setText("储值余额: " + Integer.valueOf(cardInfo.getPrepay()) / 100);
		tv_birthday.setText("生日: " + cardInfo.getBirthday());

		ArrayList<VipCoupon> arrVipCoupon = new ArrayList<>();
		arrVipCoupon.addAll(cardInfo.getCashCouponList());
		arrVipCoupon.addAll(cardInfo.getDiscountCouponList());
		arrVipCoupon.addAll(cardInfo.getGiftCouponList());
		mVipCouponAdapter.setData(arrVipCoupon);
	}

	@Override
	public void preConsumeSucess() {
		DoConsumeDialog doConsumeDialog = new DoConsumeDialog(getContext(), mCardNo, mBillAmount, mPrePay, mCash, mVipCoupon, mVipInfoPresenter);
		doConsumeDialog.show();
	}

	@Override
	public void doConsumeSucess() {
		rl_login.setVisibility(View.VISIBLE);
		rl_info.setVisibility(View.GONE);
		et_prePay.setText("");
		et_cash.setText("");
		mVipCoupon = null;
		for (int i = 0; i < mVipCouponAdapter.getCount(); i++) {
			mVipCouponAdapter.getItem(i).setSelect(false);
		}
		mVipCouponAdapter.notifyDataSetChanged();

	}


	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void showNetError(EmptyLayout.OnRetryListener onRetryListener) {

	}

	@Override
	public <T> LifecycleTransformer<T> bindToLife() {
		return null;
	}


}
